<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/21
 * Time: 14:33
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class BigcommerceApi extends Admin_Controller
{

    public function bigcommerce_add(){
        $this->load->library('sys_message');
        $assoc = $this->uri->uri_to_assoc();
        $goods_id = intval($assoc['goods_id']);
        $web_id = intval($this->input->post('web_id'));
        $bigcom = $this->db->from('shopify_goods')->where(['web_id'=>$web_id,'goods_id'=>$goods_id])->get()->row_array();
        if($bigcom){
            $this->sys_message->wrong('已上传过!');
        }
        $siteweb = $this->db->from('siteweb')->where(['web_id'=>$web_id])->get()->row_array();
        $this->load->library('bigcommerce_api',$siteweb);
        $goods = $this->db->from('goods')->where(['goods_id'=>$goods_id])->get()->row_array();
        //查询SKU
        $this->load->model('goods_model');
        $goods_sku = $this->goods_model->get_goods_sku($goods_id, 1);
        if(empty($goods_sku)){
            $this->sys_message->wrong('SKU不存在!');
        }
        $img = $this->db->from('goods_album')->where(['goods_id'=>$goods_id,'is_delete'=>0])->get()->result_array();
        $shopify_goods['goods_id'] = $goods_id;
        $shopify_goods['goods_sn'] = $goods['goods_sn'];
        $shopify_goods['web_id'] = $web_id;
        $shopify_goods['goods_name'] = $goods['goods_name'];
        $shopify_goods['cat_id'] = $goods['cat_id'];
        $shopify_goods['shop_price'] = $goods['shop_price'];
        $shopify_goods['market_price'] = $goods['market_price'];
        $shopify_goods['body_html'] = $goods['body_html'];
        $shopify_goods['images'] = json_encode($img);
        $shopify_goods['add_time'] = time();
        $this->db->insert('shopify_goods',$shopify_goods);
        $sg_id = $this->db->insert_id();
        $data_string['name'] = $goods['goods_name'];
        $data_string['price'] = $goods['shop_price'];
        $data_string['categories'] = array(25);
        $data_string['weight'] = $goods['goods_weight'];
        $data_string['type'] = 'physical';
        $data_string['description'] = $goods['body_html'];


        foreach ($goods_sku as $sku_key => $sku_val) {
            $variants[] = array(
                'sku'=>'SKU-'.$sku_val['color'].'-'.$sku_val['size'],
                'option_values'=>array(
                    array(
                        'option_display_name' => 'Color',
                        'label' => $sku_val['color'],
                    ),
                    array(
                        'option_display_name' => 'Size',
                        'label' => $sku_val['size'],
                    )
                ),
            );
        }
        $data_string['variants'] = $variants;

        foreach ($img as $k => $v) {
            if($k == 0){
                $aa = ['image_url'=>'http://sumerbikini.com'.$v['original_img'],'is_thumbnail'=>true];
            }else{
                $aa = ['image_url'=>'http://sumerbikini.com'.$v['original_img']];
            }
            $data_string['images'][] = $aa;
        }
        
        $data = $this->bigcommerce_api->Bigcommerce_add_goods($data_string);
        if(empty($data['data'])){
            $this->db->where(['goods_id'=>$goods_id,'web_id'=>$web_id]);
            $this->db->update('shopify_goods',['shopify_id'=>$data['data']['id'],'variants'=>json_encode($variants)]);
            $this->sys_message->success('成功!');
        }else{
            $this->db->delete('shopify_goods',['sg_id'=>$sg_id]);
            $this->sys_message->wrong($data['title']);
        }
    }
}




